package org.example.redisdemo.service.impl;

import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.CacheUpdate;
import com.alicp.jetcache.anno.Cached;
import org.example.redisdemo.mapper.TestMapper;
import org.example.redisdemo.model.Test;
import org.example.redisdemo.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description TestServiceImpl
 * @Author Zhang Xin
 * @Date 2020/7/3
 */
@Service
public class TestServiceImpl implements TestService {
    @Autowired
    private TestMapper testMapper;

    @Override
    @Cached(name = "TestCache", key = "#id", cacheType = CacheType.REMOTE, expire = 15)
    public Test getTest(int id) {
        return testMapper.getById(id);
    }

    @Override
    @CacheUpdate(name="TestCache", key="#value.id", value="#value")
    public void upDateTest(Test value) {
        testMapper.updateById(value);
    }

    /**
     * 调用函数 targetObject.process()
     * @Param: [id]
     * @Return: org.example.redisdemo.model.Test
     */
    @Override
    @Cached(name = "TestCache", key = "targetObject.process(#id)", cacheType = CacheType.REMOTE)
    public Test getTest2(int id) {
        return testMapper.getById(id);
    }

    public String process(int s){

        System.out.println(s);

        return "process"+s;
    }

}
